﻿using System.Runtime.CompilerServices;

namespace Known.EntityFramework;

class EFCoreHelper
{
    internal static DatabaseType GetDatabaseType(string providerName)
    {
        if (providerName.EndsWith("Sqlite"))
            return DatabaseType.SQLite;
        else if (providerName.EndsWith("SqlServer"))
            return DatabaseType.SqlServer;
        return DatabaseType.Other;
    }

    internal static FormattableString GetFormattableString(string sql, object param = null)
    {
        if (param == null)
            return FormattableStringFactory.Create(sql);

        var arguments = new List<object>();
        var properties = param.GetType().GetProperties();
        foreach (var property in properties)
        {
            if (sql.Contains(property.Name))
                arguments.Add(property.GetValue(param));
        }
        return FormattableStringFactory.Create(sql, [.. arguments]);
    }
}